//
//  MailViewController.m
//  parent
//
//  Created by yanzhen on 15/7/19.
//  Copyright (c) 2015年 ci123. All rights reserved.
//

#import "MailViewController.h"
#import "AFAppClient.h"
#import "MailTableViewCell.h"
#import "DetailViewController.h"
#import "Config.h"

@interface MailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property NSArray *data;
@end

@implementation MailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"信箱";
    _data = [NSArray new];
    [self CustomBackButton];
    [self initData];
}

-(void)initData
{
    [self showHud:@"加载数据中..." isDim:YES];
    NSDictionary *param = @{@"uid":@([[Config Instance]getUID])};
    [[AFAppClient sharedManager]getWithURL:[NSString stringWithFormat:@"%@%@",SERVER_URL_NEW,kGetMailList] withParameters:param success:^(NSDictionary *responseObject) {
        NSUInteger code = [[responseObject objectForKey:@"code"]integerValue];
        if(code==0){
            [self showHudComplete:@"您的邮箱为空哦" success:YES];
        }else if(code ==1){
            [self hideHud2];
            _data = [responseObject objectForKey:@"result"];
            [_tableview reloadData];
        }
    } failure:^(NSError *error) {
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma TableView 相关

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01f;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"MailTableViewCell";
    MailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nibArr = [[NSBundle mainBundle]loadNibNamed:@"MailTableViewCell" owner:self options:nil];
        cell = (MailTableViewCell *)[nibArr objectAtIndex:0];
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    NSDictionary *article;
    article = [_data objectAtIndex:indexPath.row];
    
    
    NSString *title = [article objectForKey:@"title"];
    cell.title.text = title;
    
    
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@",indexPath);
    //该方法响应列表中行的点击事件
    NSInteger row = indexPath.row;
    NSDictionary *dict= [_data objectAtIndex:row];
    
    NSUInteger pid = [[dict objectForKey:@"id"]integerValue ];
    DetailViewController *detail = [[DetailViewController alloc]init];
    detail.did =pid;
    detail.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detail animated:YES];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_data count];
}

//设置Cell行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 104;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
